package model;

import com.example.menhoo.toollibrary.common.assist.Check;
import com.example.menhoo.toollibrary.common.helper.AndroidFileUtil;
import com.example.menhoo.toollibrary.common.helper.DateUtil;
import com.lidroid.xutils.db.annotation.Id;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

/**
 * Created by yanglei on 15/11/8.
 */
public class DownloadDbModel {
    @Id
    public String url;
    public String path;
    public String name;
    public String tag;
    /**
     * 下载任务的状态,start,stop,complete
     */
    public String status="stop";
    public boolean isComplete=false;
    public long currDownloadSize;
    public long size;
    public Date createTime = new Date();
    public Date updateTime = new Date();

    public JSONObject getJsonObject() {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("name", this.name);
            jsonObject.put("path", this.path);
            jsonObject.put("currDownloadSize", this.currDownloadSize);
            jsonObject.put("size", this.size);
            jsonObject.put("tag", this.tag);
            jsonObject.put("url", this.url);
            jsonObject.put("isComplete", this.isComplete);
            jsonObject.put("createTime", DateUtil.DateToString(this.createTime,
                    DateUtil.TimeFormatOne));
            jsonObject.put("updateTime", DateUtil.DateToString(this.updateTime,
                    DateUtil.TimeFormatOne));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonObject;
    }

    /**
     * 得到后缀,如果有的话
     * @return
     */
    public String getSuffix(){
        if(Check.isEmpty(name)){
            return null;
        }
        else{
            return AndroidFileUtil.getSuffix(name);
        }
    }
}
